//index.js
const app = getApp()

Page({
  data: {
    banner:[],
    picture:'',
    ins:'null',
    search:null,                  // value 的值
    subtitle:"请输入查询内容...",  //提示内容
    list:[]
  },

  hadname:function(e){
    console.log(e)
    this.data.hadname=e.detail.value
  },

  pdinput:function(e){
    console.log(e)
    this.data.rolename=e.detail.value
  },

  sexinput:function(e){
    console.log(e)
    this.data.sex=e.detail.value
  },

  moneyinput:function(e){
    console.log(e)
    this.data.money=e.detail.value
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.getname}).set({
      data:{money:this.data.money}
    })
    
    
  },

  strinput:function(e){
    console.log(e)
    this.data.str=e.detail.value
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.getname}).update({
      data:{str:this.data.str}
    })
  },

  sizinput:function(e){
    console.log(e)
    this.data.siz=e.detail.value
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.getname}).update({
      data:{siz:this.data.siz}
    })
  },

  dexinput:function(e){
    console.log(e)
    this.data.dex=e.detail.value
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.getname}).update({
      data:{dex:this.data.dex}
    })
  },

  appinput:function(e){
    console.log(e)
    this.data.app=e.detail.value
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.getname}).update({
      data:{app:this.data.app}
    })
  },

  intinput:function(e){
    console.log(e)
    this.data.int=e.detail.value
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.getname}).update({
      data:{int:this.data.int}
    })
  },

  powinput:function(e){
    console.log(e)
    this.data.pow=e.detail.value
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.getname}).update({
      data:{pow:this.data.pow}
    })
  },

  eduinput:function(e){
    console.log(e)
    this.data.edu=e.detail.value
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.getname}).update({
      data:{edu:this.data.edu}
    })
  },

ins:function(e){
  console.log(e)
 
},

  onLoad: function() {
    

     
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:'沈哲范'})


    banner.get({
     success(res){
       console.log(res.data)
       this.setData({banner:res.data})
     }
  })

  banner.get().then(res=>{
    console.log(res)
    this.setData({banner:res.data})
  })
  .catch(err=>{console.log(err)})
  },

  getUserProfile() {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          avatarUrl: res.userInfo.avatarUrl,
          userInfo: res.userInfo,
          hasUserInfo: true,
        })
      }
    })
  },

  onGetUserInfo: function(e) {
    if (!this.data.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
      })
    }
  },

  onGetOpenid: function() {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
        wx.navigateTo({
          url: '../userConsole/userConsole',
        })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
        wx.navigateTo({
          url: '../deployFunctions/deployFunctions',
        })
      }
    })
  },

downloadImages:function(e){
wx.cloud.downloadFile({
  fileID:'cloud://czhdehuanjing-1g7q2idm5c2e5e43.637a-czhdehuanjing-1g7q2idm5c2e5e43-1305293077/upload/my-image.jpg'
}).then(res=>{console.log(res.tempFilePath)
this.setData({
  picture:res.tempFilePath
})}).catch(error=>{})
},

deleteImages:function(e){
wx.cloud.deleteFile({
  fileList:['cloud://czhdehuanjing-1g7q2idm5c2e5e43.637a-czhdehuanjing-1g7q2idm5c2e5e43-1305293077/upload/my-image.jpg']
}).then(res=>{console.log(res.fileList)
  picture:res.fileList
}).catch(error=>{})
},

  // 上传图片
  doUpload: function () {
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res)
        const tempFilePaths = res.tempFilePaths[0]
        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]
        
        // 上传图片
        const cloudPath = `my-image${filePath.match(/\.[^.]+?$/)[0]}`
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)

            app.globalData.fileID = res.fileID
            app.globalData.cloudPath = cloudPath
            app.globalData.imagePath = filePath
            
            wx.navigateTo({
              url: '../storageConsole/storageConsole'
            })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },
      fail: e => {
        console.error(e)
      }
    })
  },



  getname:function(res){
   
    console.log(res.detail.value)

   this.setData({
     banner:[],
     search:res.detail.value
   })
    const db = wx.cloud.database()
    const banner = db.collection('role').where({name:this.data.hadname})

    

    banner.get({
     success(res){
       console.log(res.data)
       this.setData({banner:res.data})
     }
  })

  banner.get().then(res=>{
    console.log(res)
    this.setData({banner:res.data})
  })
  .catch(err=>{console.log(err)})
  }



})
